package com.chenpeijian.demo.service;

import com.burukeyou.uniapi.http.core.channel.HttpApiMethodInvocation;
import com.burukeyou.uniapi.http.core.channel.HttpSender;
import com.burukeyou.uniapi.http.core.request.HttpBody;
import com.burukeyou.uniapi.http.core.request.UniHttpRequest;
import com.burukeyou.uniapi.http.core.response.UniHttpResponse;
import com.burukeyou.uniapi.http.extension.processor.HttpApiProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @program: student_code
 * @description:
 * @author: 陈培建
 * @create: 2025-01-17 15:01
 **/
@Slf4j
@Component
public class MTuanHttpApiProcessor implements HttpApiProcessor<MTuanHttpApi> {
    /**
     *  渠道方分配的公钥
     */
    @Value("${channel.mtuan.publicKey}")
    private String publicKey;

    @Value("${channel.mtuan.appId}")
    private String appId;
    @Value("${channel.mtuan.url}")
    private String domain;
    @Autowired
    private WeatherApi weatherApi;
    /** 实现-postBeforeHttpMetadata： 发送Http请求之前会回调该方法，可对Http请求体的内容进行二次处理
     *
     * @param uniHttpRequest              原来的请求体
     * @param methodInvocation          被代理的方法
     *                                  @return                          新的请求体
     */
    @Override
    public UniHttpRequest postBeforeHttpRequest(UniHttpRequest uniHttpRequest, HttpApiMethodInvocation<MTuanHttpApi> methodInvocation) {
        /**
         * 在查询参数中添加提供的appId字段
         */
        // 获取MTuanHttpApi注解
        MTuanHttpApi apiAnnotation = methodInvocation.getProxyApiAnnotation();
        String appIdVar = apiAnnotation.appId();
        //log.info("MTuanHttpApi appId" + appIdVar);
        //log.info("MTuanHttpApi url" + apiAnnotation.url());
        //uniHttpRequest.setUrl("http://127.0.0.1:8888");
        log.info("uniHttpRequest url" +uniHttpRequest.getHttpUrl().getUrl());
        /**
         *  生成签名sign字段
         */
        // 获取所有查询参数
        Map<String, Object> queryParam = uniHttpRequest.getHttpUrl().getQueryParam();

        // 获取请求体参数
        HttpBody body = uniHttpRequest.getBody();

        // 生成签名
        String signKey = createSignKey(queryParam,body);

        // 将签名添加到请求头中
        uniHttpRequest.putHeader("sign",signKey);

        return uniHttpRequest;
    }
    private String createSignKey(Map<String, Object> queryParam, HttpBody body) {
        // 伪代码
        return "signKey";
    }



    /**
     *  实现-postBeforeHttpMetadata： 发送Http请求时，可定义发送请求的行为 或者打印请求和响应日志。
     */
    @Override
    public UniHttpResponse postSendingHttpRequest(HttpSender httpSender, UniHttpRequest uniHttpRequest, HttpApiMethodInvocation<MTuanHttpApi> methodInvocation) {
        //log.info("开始发送Http请求 响应结果:{}",rsp.toHttpProtocol());
        return HttpApiProcessor.super.postSendingHttpRequest(httpSender, uniHttpRequest, methodInvocation);
    }


    /**
     *  实现-postAfterHttpResponseBodyResult： 反序列化后Http响应体的内容后回调，可对该结果进行二次处理返回
     * @param bodyResult                     Http响应体反序列化后的结果
     * @param rsp                            原始Http响应对象
     * @param methodInvocation                         被代理的方法
     */
    @Override
    public Object postAfterHttpResponseBodyResult(Object bodyResult, UniHttpResponse rsp, HttpApiMethodInvocation<MTuanHttpApi> methodInvocation) {
        log.info("Http响应体反序列化后的结果:{}",bodyResult);
        return HttpApiProcessor.super.postAfterHttpResponseBodyResult(bodyResult, rsp, methodInvocation);
    }

}
